package com.sise.aio和bio和nio.nio.基于nio的一个通信案例;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * @author idea
 * @data 2019/1/20
 */
public class NioClient implements Runnable {


    @Override
    public void run() {
        SocketChannel socketChannel = null;
        try {
            socketChannel = SocketChannel.open();
            socketChannel.connect(new InetSocketAddress(InetAddress.getLocalHost(), 9999));
            socketChannel.configureBlocking(false);
            while (true) {
                socketChannel.write(ByteBuffer.wrap(("this is "+Thread.currentThread().getName()).getBytes()));
                Thread.sleep(2000);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
